package com.sunny.myproject.sales.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.core.base.util.CommonUtil;
import com.sunny.myproject.sales.entity.Template;
import com.sunny.myproject.sales.service.TemplateService;

@Controller
@RequestMapping("system/template")
public class TemplateAction extends BaseAction<Template>{

	private String title = "模版";
	private String action = "system/template";
	private String function = "template";

	private Logger logger = Logger.getLogger(TemplateAction.class);
	
	@Autowired
	private TemplateService service;
	
	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest arg0, HttpServletResponse arg1) {
		super.add(title, arg0);
		return "system/templateEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest arg0, HttpServletResponse arg1) {
		try {
			service.deleteByIds(super.param2(arg0));
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/system/template?list";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.returnValue(arg0,
					service.findById(super.edit(title, arg0, arg1)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "system/templateEdit";
	}

	@Override
	@RequestMapping(params="list")
	public String list(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.pager = super.pageStart(arg0, arg1, arg2, action, function,
					title);
			super.pager = super.pageEnd(arg0, service.num(criterions), pager);
			super.returnValue(arg0, service.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "system/templateList";
	}

	@Override
	@RequestMapping(params="save")
	public String save(HttpServletRequest arg0, HttpServletResponse arg1,
			Template arg2) {
		try {
			logger.info("保存template");
			String content = arg2.getContent();
			logger.info("content:"+content);
			String[] strs = content.split("name=\"");
			String fields = "";
			//排除第一次	
			int i= 0;
			for (String string : strs) {
				logger.info("分割后的string:"+string);
				i++;
				if(i!=1){
					logger.info("name:"+string.substring(0, string.indexOf("\"", 0)));
					fields +=string.substring(0, string.indexOf("\"", 0))+",";
				}
			}
			arg2.setNameField(CommonUtil.subString_1(fields));
			service.save(arg2);
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/system/template?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.returnViewValue(arg0,
					service.findById(super.view(title, arg0, arg1)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "system/templateEdit";
	}

}
